﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading.Tasks;

namespace IceDog.Process10000File
{
    class Program
    {
        static void Main(string[] args)
        {
            Process1wFileSync();
            Process1wFileByTask();
            Process1wFileByParallel();
        }

        private static void Process1wFileSync()
        {
            Stopwatch watch = Stopwatch.StartNew();
            foreach (var index in Enumerable.Range(0, 10000))
            {
                long count = 0;
                for (long i = 0; i < 100000; i++)
                {
                    count += i;
                }
            }
            watch.Stop();
            Console.WriteLine("Process1wFileSync 耗时：" + watch.ElapsedMilliseconds + "ms");
        }

        static void Process1wFileByTask()
        {

            Stopwatch watch = Stopwatch.StartNew();
            Task[] tasks = new Task[10000];
            foreach (var index in Enumerable.Range(0, 10000))
            {
                tasks[index] = Task.Factory.StartNew(() =>
                  {
                      long count = 0;
                      for (long i = 0; i < 100000; i++)
                      {
                          count += i;
                      }
                  });

            }
            Task.WaitAll(tasks);
            watch.Stop();
            Console.WriteLine("Process1wFileByTask耗时：" + watch.ElapsedMilliseconds + "ms");
        }

        static void Process1wFileByParallel()
        {
            Stopwatch watch = Stopwatch.StartNew();
            Task[] tasks = new Task[10000];
            ParallelLoopResult result = Parallel.For(0, 10000, (index, state) =>
                {
                    long count = 0;
                    for (long i = 0; i < 100000; i++)
                    {
                        count += i;
                    }
                });
            Console.WriteLine("IsCompleted:" + result.IsCompleted);
            watch.Stop();
            Console.WriteLine("Process1wFileByParallel耗时：" + watch.ElapsedMilliseconds + "ms");
        }
    }
}
